import { Recordable } from 'vite-plugin-mock'
import { ResultEnum } from './enums/httpEnum'

export const API_PREFIX = '/api'

export function resultSuccess<T = Recordable>(result?: T, { message = 'ok', code = ResultEnum.SUCCESSCODE } = {}) {
  return {
    code,
    result,
    message
  }
}

export function resultError(message = 'Request failed', { code = ResultEnum.ERROR, result = null } = {}) {
  return {
    code,
    result,
    message
  }
}

export function pagination<T = any>(pageNo: number, pageSize: number, array: T[]): T[] {
  const offset = (pageNo - 1) * Number(pageSize)
  return offset + Number(pageSize) >= array.length
    ? array.slice(offset, array.length)
    : array.slice(offset, offset + Number(pageSize))
}
